package zarzadzaniepracownikami;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;


public class PossitionMenagment extends JFrame implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 5560806687472877351L;
	private JLabel lblWorekerName;
	private JComboBox possitionBox;
	private JButton btnConfirm;
	private JButton btnCancel;
	private Worker worker;
	private Vector<String> possition;
	private ServerConnector connector;
	private DefaultListModel workerModel;
	private Vector<Worker> listWorkers;
	/**
	 * Create the panel.
	 */
	public PossitionMenagment(String title,Vector<Worker> listWorkers, DefaultListModel worker,int index) {
		setResizable(false);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setAlwaysOnTop(true);
		

		this.listWorkers= new Vector<Worker>();
		this.listWorkers=listWorkers;
		
		connector = new ServerConnector();
		
		possition=connector.GetListOfPossition();
		
		workerModel=worker;
		
		setTitle(title);
		setBounds(100, 100, 341, 180);
		getContentPane().setLayout(null);
		
		String[] buff = worker.get(index).toString().split(" ");
		
		for (Worker w : this.listWorkers) {
			if(w.getId()==Long.parseLong(buff[0])) this.worker=new Worker(w.getName(),w.getSurname(),w.getId(),w.getPosition());
		}
		
		lblWorekerName = new JLabel("New label");
		lblWorekerName.setHorizontalAlignment(SwingConstants.CENTER);
		lblWorekerName.setText(this.worker.getName()+" "+this.worker.getSurname());
		getContentPane().add(lblWorekerName);
		
		possitionBox = new JComboBox(possition);
		possitionBox.setLocation(54, 41);
		possitionBox.setSize(230, 24);
		possitionBox.setSelectedItem(this.worker.getPosition());
		getContentPane().add(possitionBox, "6, 6, 5, 1, fill, default");
		
		btnConfirm = new JButton("Zatwierdź");
		btnConfirm.setSize(107, 38);
		btnConfirm.setLocation(57, 100);
		btnConfirm.addActionListener(this);
		getContentPane().add(btnConfirm, "6, 10");
		
		btnCancel = new JButton("Anuluj");
		btnCancel.setLocation(198, 100);
		btnCancel.setSize(86, 38);
		btnCancel.addActionListener(this);
		getContentPane().add(btnCancel, "10, 10");
		

	}
	public Worker getWorker() {
		return worker;
	}
	public void setWorker(Worker worker) {
		this.worker = worker;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==btnCancel)
		{
			this.dispose();
		}
		else if(e.getSource()==btnConfirm){
			
			boolean save =false;
			
			worker.setPosition(possitionBox.getSelectedItem().toString());
			
			for (Worker work : listWorkers) {
				if(work.getId()==worker.getId()&&connector.UpdatePossition(worker)){
					save=true;
					work.setPosition(worker.getPosition());
				}
			}
			
			if(save)
			{
				for(int i =0;i<workerModel.size();i++)
				{
					String[] var = workerModel.get(i).toString().split(" ");
					if(worker.getId()==Long.parseLong(var[0])) {
						workerModel.remove(i);
						workerModel.add(i, worker.getId()+" "+worker.getName()+" "+ worker.getSurname()+ " "+worker.getPosition());
					}
				}
				this.dispose();
			}
			else JOptionPane.showMessageDialog(rootPane, "Stanowisko nie zostało zmienione");
		}
		
	}

}
 
